<?php

namespace App\Http\Controllers\Backend;

use App\DataTables\ChannelOrderDataTable;
use App\Http\Requests;
use App\Http\Requests\CreateChannelOrderRequest;
use App\Http\Requests\UpdateChannelOrderRequest;
use App\Repositories\ChannelOrderRepository;
use Flash;
use App\Http\Controllers\AppBaseController;
use Response;

class ChannelOrderController extends AppBaseController
{
    /** @var  ChannelOrderRepository */
    private $channelOrderRepository;

    public function __construct(ChannelOrderRepository $channelOrderRepo)
    {
        $this->channelOrderRepository = $channelOrderRepo;
    }

    /**
     * Display a listing of the ChannelOrder.
     *
     * @param ChannelOrderDataTable $channelOrderDataTable
     * @return Response
     */
    public function index(ChannelOrderDataTable $channelOrderDataTable)
    {
        return $channelOrderDataTable->render('backend.channel_orders.index');
    }

    /**
     * Show the form for creating a new ChannelOrder.
     *
     * @return Response
     */
    public function create()
    {
        return view('backend.channel_orders.create');
    }

    /**
     * Store a newly created ChannelOrder in storage.
     *
     * @param CreateChannelOrderRequest $request
     *
     * @return Response
     */
    public function store(CreateChannelOrderRequest $request)
    {
        $input = $request->all();

        $channelOrder = $this->channelOrderRepository->create($input);

        Flash::success(trans('flash.controller.save_success', ['model' => 'Channel Order']));

        return redirect(route('channelOrders.index'));
    }

    /**
     * Display the specified ChannelOrder.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $channelOrder = $this->channelOrderRepository->findWithoutFail($id);

        if (empty($channelOrder)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Channel Order']));

            return redirect(route('channelOrders.index'));
        }

        return view('backend.channel_orders.show')->with('channelOrder', $channelOrder);
    }

    /**
     * Show the form for editing the specified ChannelOrder.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $channelOrder = $this->channelOrderRepository->findWithoutFail($id);

        if (empty($channelOrder)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Channel Order']));

            return redirect(route('channelOrders.index'));
        }

        return view('backend.channel_orders.edit')->with('channelOrder', $channelOrder);
    }

    /**
     * Update the specified ChannelOrder in storage.
     *
     * @param  int              $id
     * @param UpdateChannelOrderRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateChannelOrderRequest $request)
    {
        $channelOrder = $this->channelOrderRepository->findWithoutFail($id);

        if (empty($channelOrder)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Channel Order']));

            return redirect(route('channelOrders.index'));
        }

        $channelOrder = $this->channelOrderRepository->update($request->all(), $id);

        Flash::success(trans('flash.controller.update_success', ['model' => 'Channel Order']));

        return redirect(route('channelOrders.index'));
    }

    /**
     * Remove the specified ChannelOrder from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $channelOrder = $this->channelOrderRepository->findWithoutFail($id);

        if (empty($channelOrder)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Channel Order']));

            return redirect(route('channelOrders.index'));
        }

        $this->channelOrderRepository->delete($id);

        Flash::success(trans('flash.controller.delete_success', ['model' => 'Channel Order']));

        return redirect(route('channelOrders.index'));
    }
}
